package com.mqqzt.douban;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class MainTabActivity extends TabActivity {
	private TabHost mTabHost;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_tab);
		mTabHost = getTabHost();
		mTabHost.addTab(getMyDoubanTab());
		mTabHost.addTab(getNewBookTab());
	}
	
	private TabSpec getMyDoubanTab(){
		TabSpec spec = mTabHost.newTabSpec("mydouban");
		Intent intent = new Intent(this, MeActivity.class);
		spec.setContent(intent);
		spec.setIndicator(getIndicatorView("我的豆瓣", R.drawable.tab_main_nav_me));
		return spec;
	}
	
	private TabSpec getNewBookTab(){
		TabSpec spec = mTabHost.newTabSpec("newbook");
		Intent intent = new Intent(this, NewBookActivity.class);
		spec.setContent(intent);
		spec.setIndicator(getIndicatorView("豆瓣新书", R.drawable.tab_main_nav_book));
		return spec;
	}
	
	private View getIndicatorView(String name, int iconid){
		View view = View.inflate(this, R.layout.tab_main_nav, null);
		ImageView ivIcon = (ImageView) view.findViewById(R.id.ivIcon);
		TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
		ivIcon.setImageResource(iconid);
		tvTitle.setText(name);
		return view;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = new MenuInflater(this);
		inflater.inflate(R.menu.main_tab_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.main_tab_menu_clear_user:
			SharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);
			Editor editor = sp.edit();
			editor.putString("accesstoken", "");
			editor.putString("tokensecret", "");
			editor.commit();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
}
